/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InputMk.java
 *
 * Created on Oct 19, 2010, 7:48:17 PM
 */
package stta.bahrie.admin.gui;

import api.stta.bahrie.entiy.Dosen;
import api.stta.bahrie.entiy.Mk;
import api.stta.bahrie.inter.DosenInterf;
import api.stta.bahrie.inter.MkInterf;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author bahrie
 */
public class InputMk extends javax.swing.JInternalFrame {

    MkInterf mkService;
    List<Mk> records = new ArrayList<Mk>();
    int row;
    DosenInterf dsnService;
    List<Dosen> listDosen = new ArrayList<Dosen>();

    /** Creates new form DataMk */
    public InputMk(MkInterf mkService, DosenInterf dsnService) {
        try {
            this.mkService = mkService;
            this.dsnService = dsnService;
            initComponents();
            this.statusAwal();
            tableMk.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

                public void valueChanged(ListSelectionEvent e) {
                    row = tableMk.getSelectedRow();
                    if (row != -1) {
                        isitext();
                    }
                }
            });
        } catch (RemoteException ex) {
            Logger.getLogger(InputMk.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    void loadDosen() throws RemoteException {
        listDosen = dsnService.getAll();
    }

    void isiListDosen() {
        comboDosen.removeAllItems();
        for (Dosen dsn : listDosen) {
            comboDosen.addItem(dsn.getDosen());
        }
    }

    void kosongkanText() {
        txtKdMk.setText("");
        txtMk.setText("");
        txtJurusan.setText("");
        comboDosen.removeAllItems();
        for (Dosen dsn : listDosen) {
            comboDosen.addItem(dsn.getDosen());
        }
        txtSks.setText("");
    }

    void isiTabelMk() {
        try {
            Object[][] data = new Object[records.size()][5];

            int x = 0;
            for (Mk mk : records) {
                data[x][0] = mk.getKd_mk();
                data[x][1] = mk.getMk();
                data[x][2] = mk.getSks();
                data[x][3] = mk.getJurusan();
                data[x][4] = mk.getKd_dosen();
                x++;
            }
            String[] judul = {"Kode Mk", "Mk", "Sks", "Jurusan", "Kd Dosen"};
            tableMk.setModel(new DefaultTableModel(data, judul));
            jScrollPane1.setViewportView(tableMk);
        } catch (Throwable ex) {
            ex.printStackTrace();
        }
    }

    private void loadRecords() throws RemoteException {
        records = mkService.getAll();
        this.isiTabelMk();
    }

    void isitext() {
        Mk mk = records.get(row);
        txtKdMk.setText(mk.getKd_mk());
        txtMk.setText(mk.getMk());
        txtSks.setText(String.valueOf(mk.getSks()));
        txtJurusan.setText(mk.getJurusan());
        comboDosen.removeAllItems();
        for (Dosen dsn : listDosen) {
            comboDosen.addItem(dsn.getDosen());
        }
    }

    private void statusAwal() throws RemoteException {
        this.loadRecords();
        this.kosongkanText();
        this.loadDosen();
        this.isiListDosen();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtKdMk = new javax.swing.JTextField();
        txtMk = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtJurusan = new javax.swing.JTextField();
        txtSks = new javax.swing.JTextField();
        comboDosen = new javax.swing.JComboBox();
        jPanel3 = new javax.swing.JPanel();
        inputButton = new javax.swing.JButton();
        hapusButton = new javax.swing.JButton();
        updateBotton = new javax.swing.JButton();
        resetBotton = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableMk = new javax.swing.JTable();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Input Mata Kuliah");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Input MataKuliah"));

        jLabel1.setText("Kode Mk:");

        jLabel2.setText("Mata Kuliah :");

        jLabel5.setText("Sks :");

        jLabel3.setText("Jurusan :");

        jLabel4.setText("Dosen :");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtMk, javax.swing.GroupLayout.DEFAULT_SIZE, 295, Short.MAX_VALUE)
                    .addComponent(txtKdMk, javax.swing.GroupLayout.DEFAULT_SIZE, 295, Short.MAX_VALUE)
                    .addComponent(txtJurusan, javax.swing.GroupLayout.DEFAULT_SIZE, 295, Short.MAX_VALUE)
                    .addComponent(txtSks, javax.swing.GroupLayout.DEFAULT_SIZE, 295, Short.MAX_VALUE)
                    .addComponent(comboDosen, 0, 295, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtKdMk, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtMk, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtSks, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtJurusan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(comboDosen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(16, Short.MAX_VALUE))
        );

        inputButton.setText("Input");
        inputButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                inputButtonActionPerformed(evt);
            }
        });
        jPanel3.add(inputButton);

        hapusButton.setText("Hapus");
        hapusButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hapusButtonActionPerformed(evt);
            }
        });
        jPanel3.add(hapusButton);

        updateBotton.setText("Update");
        updateBotton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateBottonActionPerformed(evt);
            }
        });
        jPanel3.add(updateBotton);

        resetBotton.setText("Reset");
        resetBotton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetBottonActionPerformed(evt);
            }
        });
        jPanel3.add(resetBotton);

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Tabel Matakuliah"));

        tableMk.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tableMk);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 389, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 135, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 425, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void inputButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_inputButtonActionPerformed
        try {
            // TODO add your handling code here:
            Mk mk = new Mk();
            mk.setKd_mk(txtKdMk.getText());
            mk.setMk(txtMk.getText());
            mk.setSks(Integer.valueOf(txtSks.getText()));
            mk.setJurusan(txtJurusan.getText().toUpperCase());
            mk.setKd_dosen(listDosen.get(comboDosen.getSelectedIndex()).getKd_dosen());
            mkService.insert(mk);
            this.statusAwal();
        } catch (RemoteException ex) {
            Logger.getLogger(InputMk.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_inputButtonActionPerformed

    private void hapusButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hapusButtonActionPerformed
        try {
            // TODO add your handling code here:
            String kd = records.get(tableMk.getSelectedRow()).getKd_mk();
            mkService.delete(kd);
            this.statusAwal();
        } catch (RemoteException ex) {
            Logger.getLogger(InputMk.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_hapusButtonActionPerformed

    private void updateBottonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateBottonActionPerformed
        try {
            // TODO add your handling code here:

            Mk mk = records.get(tableMk.getSelectedRow());
            mk.setKd_mk(txtKdMk.getText());
            mk.setMk(txtMk.getText());
            mk.setSks(Integer.valueOf(txtSks.getText()));
            mk.setJurusan(txtJurusan.getText());
            mk.setKd_dosen(listDosen.get(comboDosen.getSelectedIndex()).getKd_dosen());
            mkService.update(mk);
            this.statusAwal();
        } catch (RemoteException ex) {
            Logger.getLogger(InputMk.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_updateBottonActionPerformed

    private void resetBottonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetBottonActionPerformed
        try {
            // TODO add your handling code here:
            this.statusAwal();
        } catch (RemoteException ex) {
            Logger.getLogger(InputMk.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_resetBottonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox comboDosen;
    private javax.swing.JButton hapusButton;
    private javax.swing.JButton inputButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton resetBotton;
    private javax.swing.JTable tableMk;
    private javax.swing.JTextField txtJurusan;
    private javax.swing.JTextField txtKdMk;
    private javax.swing.JTextField txtMk;
    private javax.swing.JTextField txtSks;
    private javax.swing.JButton updateBotton;
    // End of variables declaration//GEN-END:variables
}
